package com.ede.roverhu.simplebackstage.Engine;

import android.content.Context;

import com.ede.roverhu.simplebackstage.Domain.ClassListDomain;
import com.ede.roverhu.simplebackstage.Domain.SchoolBase;
import com.ede.roverhu.simplebackstage.Domain.UserInfoBase;
import com.ede.roverhu.simplebackstage.Domain.UserInfoExample;
import com.ede.roverhu.simplebackstage.Domain.schoolClassBase;
import com.ede.roverhu.simplebackstage.Domain.timeTableClassInfoDomain;
import com.ede.roverhu.simplebackstage.Utils.LogUtils;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;

/**
 * Created by roverhu on 2017/6/15.
 */

public class htmlHander {

    public static ArrayList<UserInfoBase> getPeopleList(Context context, String html) {
        ArrayList<UserInfoBase> List = new ArrayList<>();
        LogUtils.e(html);
        Document document = Jsoup.parse(html, "UTF-8");
        Element info = document.body().select(".datalist").first();
        Elements info1 = info.select("tr");
        info.empty();
        for (int i = 1; i < info1.size() - 2; i++) {
            UserInfoBase userInfoBase = trHandler(info1.get(i));
            List.add(userInfoBase);
        }
        info1.empty();
        return List;
    }

    public static UserInfoBase trHandler(Element e) {
        UserInfoBase userInfoBase = new UserInfoBase();
        Elements info2 = e.select("td");
//        LogUtils.e(info2.get(1).text());
        userInfoBase.iD = info2.get(1).text();
        userInfoBase.headImg = info2.get(2).text();
        userInfoBase.trueName = info2.get(3).text();
        userInfoBase.nickName = info2.get(4).text();
        userInfoBase.stuId = info2.get(5).text();
        userInfoBase.phoneNum = info2.get(6).text();
        userInfoBase.sex = info2.get(7).text();
        userInfoBase.school = info2.get(8).text();
        userInfoBase.Class = info2.get(9).text();
        userInfoBase.xuenian = info2.get(10).text();
        userInfoBase.xueqi = info2.get(11).text();
        userInfoBase.update = info2.get(12).text();
        return userInfoBase;
    }

    public static ArrayList<SchoolBase> getSchoolList(Context context, String str) {
        ArrayList<SchoolBase> list = new ArrayList<>();
//        File file = new File(context.getCacheDir(), "cache");
        Document document = Jsoup.parse(str, "UTF-8");
        Element info = document.body().select("select").first();
        Elements info1 = info.select("option");
        for (Element info2 : info1) {
            SchoolBase schoolBase = new SchoolBase();
            schoolBase.sid = info2.attr("value");
//                LogUtils.e(schoolBase.sid);
            schoolBase.name = info2.text();
//                LogUtils.e(schoolBase.name);
            list.add(schoolBase);
        }
        return list;
    }


    public static String getUserinfoHTML(Context context, String str) {
        //UserInfoExample userInfoExample = new UserInfoExample();
        Document document = Jsoup.parse(str, "UTF-8");
        document.body().select("div.mypath").remove();
        document.body().select("div#header").remove();
        Element info = document.body().select("form#frmDemo").first();
        String s = info.attr("action");
        s = "http://wise.xcollege.cn/ms_admin/app/member.php" + s;
        LogUtils.d(s);
        info.attr("action", s);
        return document.toString();
    }

    public static String getClassinfoHTML(Context context, String str) {
        //UserInfoExample userInfoExample = new UserInfoExample();
        Document document = Jsoup.parse(str, "UTF-8");
        document.body().select("div.mypath").remove();
        document.body().select("div#header").remove();
        Element info = document.body().select("form#frmDemo").first();
        String s = info.attr("action");
        s = "http://wise.xcollege.cn/ms_admin/app/study.php" + s;
        LogUtils.d(s);
        info.attr("action", s);
        //去除article tr
        Elements elements = info.select("tr");
        elements.get(elements.size() - 2).remove();
        elements.get(elements.size() - 3).remove();
        return document.toString();
    }

    public static String getCourseinfoHTML(Context context, String str) {
        //UserInfoExample userInfoExample = new UserInfoExample();
        LogUtils.e(str);
        Document document = Jsoup.parse(str, "UTF-8");
        document.body().select("div.mypath").remove();
        document.body().select("div#header").remove();
        Element info = document.body().select("form#frmDemo").first();
        String s = info.attr("action");
        s = "http://wise.xcollege.cn/ms_admin/app/course.php" + s;
        LogUtils.d(s);
        info.attr("action", s);
        //去除article tr
        Elements elements = info.select("tr");
        for (int i = 0; i < 17; i++) {
            elements.get(elements.size() - 2 - i).remove();
        }
        return document.toString();

    }


    public static schoolClassBase trHandler2(Element e) {
        schoolClassBase schoolClassBase = new schoolClassBase();
        Elements info2 = e.select("td");
        LogUtils.e(info2.get(1).text());
        schoolClassBase.iD = info2.get(1).text();
        schoolClassBase.sName = info2.get(2).text();
        schoolClassBase.tec = info2.get(3).text();
        schoolClassBase.floor = info2.get(4).text();
        schoolClassBase.classId = info2.get(5).text();
        schoolClassBase.className = info2.get(6).text();
        schoolClassBase.minor = info2.get(7).text();
        return schoolClassBase;
    }

    public static ClassListDomain trHandler3(Element e) {
        ClassListDomain ClassListDomain = new ClassListDomain();
        Elements info2 = e.select("td");
        LogUtils.e(info2.get(1).text());
        ClassListDomain.iD = info2.get(1).text();
        ClassListDomain.clName = info2.get(2).text();
        return ClassListDomain;
    }

    public static timeTableClassInfoDomain trHandler4(Element e) {
        timeTableClassInfoDomain timeTableClassInfoDomain = new timeTableClassInfoDomain();
        Elements info2 = e.select("td");
        LogUtils.e(info2.get(1).text());
        timeTableClassInfoDomain.iD = info2.get(1).text();
        timeTableClassInfoDomain.courseName = info2.get(2).text();
        timeTableClassInfoDomain.sName = info2.get(3).text();
        timeTableClassInfoDomain.yuanxi = info2.get(4).text();
        timeTableClassInfoDomain.className = info2.get(5).text();
        timeTableClassInfoDomain.time = info2.get(6).text();
        timeTableClassInfoDomain.place = info2.get(7).text();
        timeTableClassInfoDomain.classNum = info2.get(8).text();
        return timeTableClassInfoDomain;
    }

    public static ArrayList<schoolClassBase> getSchoolClass(Context context, String html) {
        ArrayList<schoolClassBase> List = new ArrayList<>();
//        File file = new File(context.getCacheDir(), "cache");
        LogUtils.e(html);
        Document document = Jsoup.parse(html, "UTF-8");
        Element info = document.body().select(".datalist").first();
        Elements info1 = info.select("tr");
        info.empty();
        for (int i = 1; i < info1.size() - 2; i++) {
            schoolClassBase scb = trHandler2(info1.get(i));
            List.add(scb);
        }
        info1.empty();
        return List;
    }

    public static ArrayList<ClassListDomain> getTimetable(Context context, String html) {
        ArrayList<ClassListDomain> List = new ArrayList<>();
        // LogUtils.e(html);
        Document document = Jsoup.parse(html, "UTF-8");
        Element info = document.body().select(".datalist").first();
        Elements info1 = info.select("tr");
        info.empty();
        for (int i = 1; i < info1.size() - 2; i++) {
            ClassListDomain scb = trHandler3(info1.get(i));
            List.add(scb);
        }
        info1.empty();
        return List;
    }

    /**
     * 获取课表查询页面的options，并转化成list
     *
     * @param context
     * @param html
     * @return
     */
    public static ArrayList<schoolClassBase> getTimetableClass(Context context, String html) {
        ArrayList<schoolClassBase> list = new ArrayList<>();
//        File file = new File(context.getCacheDir(), "cache");
        Document document = Jsoup.parse(html, "UTF-8");
        Element info = document.body().select("select").first();
        Elements info1 = info.select("option");
        for (Element info2 : info1) {
            schoolClassBase schoolClassBase = new schoolClassBase();
            schoolClassBase.classId = info2.attr("value");
//                LogUtils.e(schoolBase.sid);
            schoolClassBase.className = info2.text();
//                LogUtils.e(schoolBase.name);
            list.add(schoolClassBase);
        }
        return list;
    }

    /**
     * 获取课表详细列表
     *
     * @param context
     * @param html
     * @return
     */
    public static ArrayList<timeTableClassInfoDomain> getTimetableInfoList(Context context, String html) {
        ArrayList<timeTableClassInfoDomain> List = new ArrayList<>();
        // LogUtils.e(html);
        Document document = Jsoup.parse(html, "UTF-8");
        Element info = document.body().select(".datalist").first();
        Elements info1 = info.select("tr");
        LogUtils.e(info1.get(0).toString());
        info.empty();
        for (int i = 1; i < info1.size() - 2; i++) {
            timeTableClassInfoDomain scb = trHandler4(info1.get(i));
            List.add(scb);
        }
        info1.empty();
        return List;
    }

}
